//
// SimpleDirStrainer
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.type;

import java.io.*;
import java.util.*;
import net.jextra.crack.*;
import net.jextra.crack.tool.*;

public class SimpleDirStrainer implements DirStrainer
{
    // ============================================================
    // Fields
    // ============================================================

    private ArrayList<String> paths;
    private File rootDir;

    // ============================================================
    // Constructors
    // ============================================================

    public SimpleDirStrainer()
    {
        paths = new ArrayList<String>();
    }

    public SimpleDirStrainer( String path )
    {
        paths = new ArrayList<String>();
        addPath( path );
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void addPath( String path )
    {
        String p = path;
        if ( !p.startsWith( "/" ) )
        {
            p = "/" + p;
        }

        if ( !p.endsWith( "/" ) )
        {
            p = p + "/";
        }

        paths.add( p );

    }

    @Override
    public Collection<File> getDirs( File rootDir )
    {
        assert rootDir != null : "rootDir can not be null";
        assert rootDir.isDirectory() : String.format( "rootDir [%s] must be a directory", rootDir.getAbsolutePath() );

        this.rootDir = rootDir;
        try
        {
            return getDirsRecurse( rootDir );
        }
        catch ( IOException ex )
        {
            throw new CrackException( ex );
        }
    }

    // ----------
    // private
    // ----------

    private Collection<File> getDirsRecurse( File dir )
        throws IOException
    {
        Collection<File> dirs = new LinkedHashSet<File>();

        // Add self if matches path.
        if ( matchDir( dir ) )
        {
            dirs.add( dir );
        }

        for ( File child : dir.listFiles() )
        {
            // Only return directories.
            if ( !child.isDirectory() )
            {
                continue;
            }

            // Add children.
            dirs.addAll( getDirsRecurse( child ) );
        }

        return dirs;
    }

    private boolean matchDir( File dir )
        throws IOException
    {
        String string = "/" + FileTool.getRelativePath( rootDir, dir ) + "/";

        for ( String path : paths )
        {
            if ( string.equalsIgnoreCase( path ) )
            {
                return true;
            }
        }

        return false;
    }
}
